//
//  BindCardSendCodeView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/9.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class BindCardSendCodeView: UIView, UITextFieldDelegate {
    
    weak var delegate: BindCardMainViewDelegate?

    @IBOutlet private weak var phoneLabel: UILabel!
    
    @IBOutlet private weak var splitLine: UIView!
    
    @IBOutlet private weak var codeTextField: UITextField! {
        didSet {
            codeTextField.delegate = self
        }
    }
    
    @IBAction private func getCodeAction(_ sender: CountDownButton) {
        sender.countDown(with: 60)
        delegate?.didSendCodeMsg()
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        splitLine.backgroundColor = .splLineSelect
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        splitLine.backgroundColor = .splLineNormal
        delegate?.didEndEdit(textField: textField)
    }
    
    @IBAction private func cancelAction() {
        if let delegate = delegate, delegate.responds(to: #selector(delegate.didCancelAction)) {
            delegate.didCancelAction!()
        }
    }
    
    @IBAction private func sureAction() {
        if codeTextField.text!.isEmpty {
            HUDTool.show(message: "验证码为空")
            return
        }
        
        delegate?.didSureAddCard(msgCode: codeTextField.text!)
    }
    
    func configure(with vm: BindCardViewModel) {
        phoneLabel.text = vm.phone
    }
}
